using System;
using System.Collections.Generic;
using SCG.SolarVengeanceEngine;

namespace SCG.SolarVengeance.Scenarios
{
    public class SolarWind : Scenario
    {
        private int cycles = 0;
        private ScenarioParameter pIntensity;
        private ScenarioParameter pStars;
        private ScenarioParameter pNebula;
        private ScenarioParameter pPulsars;
        private ScenarioParameter pWormHoles;
        private ScenarioParameter pBlackHoles;

        public SolarWind()
        {
            pIntensity = CreateParameter("Wind intensity", 2, 1, 5);
            pStars = CreateParameter("Star Systems", 25, 20, 100);
            pNebula = CreateParameter("Nebula", 4, 0, 10);
            pPulsars = CreateParameter("Pulsars", 4, 0, 10);
            pBlackHoles = CreateParameter("BlackHoles", 0, 0, 4);
            pWormHoles = CreateParameter("WormHoles", 4, 0, 10);
        }

        //Create the Scenario
        public override void BuildScenario(int numPlayers, int capitalValues, int startingResources)
        {
            CreateStarCluster(pStars.Value, 50, 50, 50, 50, 50, 2);
            int dNebula = pNebula.Value;
            int dWorms = pWormHoles.Value;
            int dHoles = pBlackHoles.Value;
            int dPulsars = pPulsars.Value;

            for (int s = 0; s < dNebula; s++)
            {
                CreateNebula(Random(30, 70), Random(30, 70), Random(1, 4), Random(25, 100));
            }
            for (int s = 0; s < dWorms; s++)
            {
                CreateWormhole(Random(30, 70), Random(50, 80), Random(30, 70), Random(20, 50));
            }
            for (int s = 0; s < dHoles; s++)
            {
                CreateBlackHole(Random(10, 90), Random(10, 90), Random(5, 12));
            }
            for (int s = 0; s < dPulsars; s++)
            {
                CreatePulsar(Random(30, 70), Random(30, 70));
            }
        }
        
        //Scenario Engine
        public override void  ProcessImpulse(int impulse)
        {
            //Move Nebula
            for (int x = 0; x < 100; x++)
            {
                if (Random(0, 100) >= 105 - (pIntensity.Value * 10))
                {
                    char[] terrain = new char[100];
                    for (int y = 0; y < 100; y++)
                    {
                        if (GetTerrain(x, y) == TerrainType.Nebula)
                            terrain[y] = 'N';
                        else
                            terrain[y] = ' ';
                    }
                    for (int y = 0; y < 100; y++)
                    {
                        if (y == 99)
                        {
                            DynamicEvents.SetTerrain(x,0,terrain[y]);
                        }
                        else
                        {
                            DynamicEvents.SetTerrain(x, y + 1, terrain[y]);
                        }
                    }
                }
            }

            cycles++;
            if (cycles >= 24 - (pIntensity.Value * 4))
            {
                cycles = 0;
                for (int s = Game.StarShips.Count - 1; s >= 0; s--)
                {
                    if (Game.StarShips[s].Y >= 99)
                    {
                        SVMapObject dstarship = Game.StarShips[s];
                        DynamicEvents.RemoveObject(dstarship);
                    }
                    else
                    {
                        StarShip movess = Game.StarShips[s];
                        DynamicEvents.MoveStarShip(movess, movess.X, movess.Y + 1);
                    }
                }
            }
        }

        //Scenario description
        public override string Description
        {
            get
            {
                return "This Star Cluster is feeling the effects of the Solar Wind ... ";
            }
        }

        //Author
        public override string Author
        {
            get
            {
                return "Beat2k";
            }
        }
    }
}
